<?php
	$css_list[0]['url'] = './tpl/css/article.css';
	$js_list[0] = './public/js/jquery.masonry.js';
?>
<!--{template inc/header_n}-->
<div class="main_body">

    <div class="cart">
        
        <div class="i_total_top">已选商品：<span class="i_price1 j_total_num">0</span> 件&nbsp;&nbsp;&nbsp;&nbsp;合计：<span class="i_price1 j_total_price">¥0</span></div>

        <table class="i_tb">
            <tr>
                <th width="50"><label><input type="checkbox" onclick="cateSelectAll(this)"> 全选</label></th>
                <th width="100">商品信息</th>
                <th width="220"></th>
                <th width="180"></th>
                <th>单价（元）</th>
                <th>数量</th>
                <th>金额（元）</th>
                <th>操作</th>
            </tr>
        <!--{loop $format_products $k $products}-->
            <tr id="cate_{$k}">
                <td colspan="8" class="j_tt"><label><input id="cate_c_{$k}" type="checkbox" onclick="cateSelect(this,{$k})"> {$cateinfo[$cateinfo[$k]['parent_id']]['cate_name']}-{$cateinfo[$k]['cate_name']}</label></td>
            </tr>
            <!--{loop $products $v}-->
            <tr id="p_{$v['id']}" class="j_tr" pcata="cate_{$k}">
                <td><input type="checkbox" value="{$v['id']}" onclick="productSelect({$k})" class="item_check" mygroup="cate_{$k}"></td>
                <td><a href="/product.php?action=show&id={$v['id']}" target="_blank"><img src="{$v['img']}" class="h_img"></a></td>
                <td><a href="/product.php?action=show&id={$v['id']}" target="_blank">{$v['title']}</a></td>
                <td><span class="h_para">来源：{$v['source']}</span></td>
                <td><span class="i_price item_price">{$v['price']}</span></td>
                <td><input type="button" value="-" onclick="cartNumChange(-1,{$v['id']})" class="h_btn"><input type="text" name="" class="h_ipt item_amount" size="2" value="1" dId="{$v['id']}"><input type="button" value="+" onclick="cartNumChange(1,{$v['id']})" class="h_btn"></td>
                <td><span class="i_price item_total">{$v['price']}</span></td>
                <td><a href="javascript:;" onclick="delfromcart({$v['id']},{$k})">删除</a></td>
            </tr>
            <!--{/loop}-->
        <!--{/loop}-->
        </table>

        <div class="i_total_bottom">已选商品：<span class="i_price1 j_total_num">0</span> 件&nbsp;&nbsp;&nbsp;&nbsp;合计：<span class="i_price1 j_total_price">¥0</span></div>

    </div>

</div>

<script>

function delfromcart(id,k) {
    var ids = $.cookie('pids');
    var idsarr = ids.split(',');
    for(var i = 0; i < idsarr.length; i++) {
        if(idsarr[i] == id) {
            idsarr.splice(i, 1);
        }
    }
    $.cookie('pids', idsarr.join(','));
    $('#mycartwrap').html('比价车('+idsarr.length+')');
    $('#p_'+id).remove();
    if($("tr[pcata='cate_"+k+"']").size()==0){
        $('#cate_'+k).remove();
    }
    changeTotal();
}

$(".item_amount").keyup(function(){
    var id=$(this).attr('dId');
    cartNumChange(0,id);
});

function cartNumChange(v,id){
    //var amount=parseInt($("#p_"+id+" .item_amount").val());
    var amount=$("#p_"+id+" .item_amount").val();
    if(!IsNum(amount)){
        amount=1;
    }else{
        amount=parseInt(amount);
        if(v!=0){
            amount+=v;
        }
    }
    var price=parseFloat($("#p_"+id+" .item_price").html());
    var total=0;
    
    if(amount<1) amount=1;
    total=price*amount;
    $("#p_"+id+" .item_amount").val(amount);
    $("#p_"+id+" .item_total").html(pointFomart(total));
    changeTotal();
}

function cateSelect(obj,k){
    $("tr[pcata='cate_"+k+"'] .item_check").attr("checked", obj.checked);
    changeTotal();
}

function cateSelectAll(obj){
    $("tr[id^='cate_'] input[type='checkbox']").attr("checked", obj.checked);
    $("tr[pcata^='cate_'] .item_check").attr("checked", obj.checked);
    changeTotal();
}

function changeTotal(){
    var total=0;
    var num=0;
    $(".item_check").each(function(i){
        if($(this).is(':checked')) {
            total+=parseFloat($("#p_"+$(this).val()+" .item_total").html());
            num+=parseInt($("#p_"+$(this).val()+" .item_amount").val());
        }
    });
    $(".j_total_price").html('¥'+pointFomart(total));
    $(".j_total_num").html(num);
}

function pointFomart(num){
    var rema=num-parseInt(num)
    if(rema>0){
        return num.toFixed(2);
    }else{
        return num;
    }
}


function IsNum(s)
{
   if (s!=null && s!="")
  {        return !isNaN(s);
   }
   return false;
}

function productSelect(id) {
    var b = true;
    $('[mygroup=cate_'+id+']').each(function(i, obj){
        if($(obj).attr('checked') != 'checked') {
            b = false;
        }
    });
    $('#cate_c_'+id).attr('checked',b);

    changeTotal();
}
</script>
<!--{template inc/footer}-->